//
//  BBZModifyViewController.m
//  QuickCure
//
//  Created by 周洋 on 16/9/15.
//  Copyright © 2016年 haoge. All rights reserved.
//

#import "BBZModifyViewController.h"
#import "Masonry.h"
#import "SVProgressHUD.h"
#import "LoginViewController.h"

@interface BBZModifyViewController () <UITextFieldDelegate>

@property (nonatomic, weak) UITextField *preField;

@property (nonatomic, weak) UITextField *newtField;

@property (nonatomic, weak) UITextField *confirmField;

@end

@implementation BBZModifyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
}

#pragma mark --
#pragma mark -- 设置界面
- (void)setupUI {
    
    self.navigationItem.title = @"修改密码";
    
    //原密码
    UIView *preView = [self createPasswordViewWithName:@"原密码" placeholder:@"请输入原密码" textFieldType:TextFieldTypePre];
    UIView *newView = [self createPasswordViewWithName:@"新密码" placeholder:@"请输入6~15位新密码" textFieldType:TextFieldTypeNew];
    UIView *confirmView = [self createPasswordViewWithName:@"确认密码" placeholder:@"请再次输入新密码" textFieldType:TextFieldTypeConfirm];
    [preView makeConstraints:^(MASConstraintMaker *make) {
        
        make.leading.equalTo(self.view).offset(30);
        make.top.equalTo(self.view).offset(60);
        make.trailing.equalTo(self.view).offset(-30);
        make.height.equalTo(30);
    }];
    
    [newView makeConstraints:^(MASConstraintMaker *make) {
        
        make.leading.trailing.height.equalTo(preView);
        make.top.equalTo(preView.bottom).offset(22);
    }];
    
    [confirmView makeConstraints:^(MASConstraintMaker *make) {
        
        make.leading.trailing.height.equalTo(preView);
        make.top.equalTo(newView.bottom).offset(22);
    }];
    
    //确认button
    UIButton *btn = [[UIButton alloc] init];
    
    [self.view addSubview:btn];
    
    [btn makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.equalTo(self.view);
        make.width.equalTo(@240);
        make.top.equalTo(confirmView.bottom).offset(30);
    }];
    
    btn.layer.cornerRadius = 5;
    [btn setTitle:@"提交修改" forState:UIControlStateNormal];
    [btn setBackgroundColor:[UIColor colorWithRed:0.063 green:0.717 blue:0.414 alpha:1.000]];
    [btn.titleLabel setFont:[UIFont systemFontOfSize:19]];
    
    //添加点击方法
    [btn addTarget:self action:@selector(confirmBtnClick) forControlEvents:UIControlEventTouchUpInside];
}


#pragma mark --
#pragma mark -- 提交button点击事件
- (void)confirmBtnClick {
    
    NSString *prePassword = [[NSUserDefaults standardUserDefaults] objectForKey:kPassword];
    
    //判断原密码是否正确
    if(![self.preField.text isEqualToString:prePassword]) {
        
        [self showErrorWithStatus:@"原密码输入不正确"];
    }
    else {
        
        //输入正确，修改本地密码，跳转到登录界面
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        [userDefaults removeObjectForKey:kPassword];
        [userDefaults setObject:self.newtField.text forKey:kPassword];
        [userDefaults synchronize];
        
        //先显示弹框
        [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
        [SVProgressHUD showSuccessWithStatus:@"修改成功，跳至登录界面!"];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            //登录界面的密码是空，需要手动输入新密码
            LoginViewController *VC = [[LoginViewController alloc]init];
            VC.isPasswordEmpty = YES;
            
            [self.navigationController pushViewController:VC animated:YES];
        });
    }
}

#pragma mark --
#pragma mark -- 封装创建view的代码
- (UIView *)createPasswordViewWithName:(NSString *)name placeholder:(NSString *)placeholder textFieldType:(TextFieldType)type {
    
    UIView *pwdView = [[UIView alloc] init];
    
    [self.view addSubview:pwdView];
    
    //名称label
    UILabel *label = [[UILabel alloc] init];
    
    [pwdView addSubview:label];
    
    label.font = kFONT16;
    label.text = name;
    
    [label makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.leading.bottom.equalTo(pwdView);
        make.width.equalTo(@65);
    }];
    
    //密码textField
    UITextField *tField = [[UITextField alloc] init];
    
    [pwdView addSubview:tField];
    tField.autocorrectionType = UITextAutocorrectionTypeNo;
    tField.secureTextEntry = YES;
    tField.returnKeyType = UIReturnKeyGoogle;
    tField.delegate = self;
    
    [tField makeConstraints:^(MASConstraintMaker *make) {
        
        make.leading.equalTo(label.trailing).offset(10);
        make.centerY.equalTo(pwdView);
        make.trailing.equalTo(pwdView);
    }];
    
    if(type == TextFieldTypePre) {
        
        self.preField = tField;
    }
    else if(type == TextFieldTypeNew) {
        
        self.newtField = tField;
    }
    else {
        
        self.confirmField = tField;
    }

    
    UIImageView *img = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"password_icon"]];
    img.frame = CGRectMake(5, 0, 18, 20);
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 20, 20)];
    [view addSubview:img];
    //设置左边的小图标
    tField.leftViewMode = UITextFieldViewModeAlways;
    tField.leftView = view;
    tField.borderStyle = UITextBorderStyleRoundedRect;
    tField.placeholder = placeholder;
    
    return pwdView;
}

#pragma mark --
#pragma mark -- textField代理方法
- (void)textFieldDidEndEditing:(UITextField *)textField {
    
    //输入新密码,判断密码长度
    if(textField == self.newtField) {
        
        if(textField.text.length > 15 || textField.text.length < 6) {
            
            [self showErrorWithStatus:@"密码长度不是6~15位!"];
        }
    }
    
    
    if(textField == self.confirmField) {
        
        //确认密码的textField，判断是否和新密码一样
        if(![textField.text isEqualToString:self.newtField.text]) {
            
            [self showErrorWithStatus:@"两次密码不一致!"];
        }
    }
}

#pragma mark --
#pragma mark -- 封装显示错误弹窗代码
- (void)showErrorWithStatus:(NSString *)string {
    
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    [SVProgressHUD showErrorWithStatus:string];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [SVProgressHUD dismiss];
    });
}


@end















